/*
 * Created by JFormDesigner on Fri Jun 28 16:25:43 CST 2024
 */

package scct.cuixinlong.ui;

import scct.cuixinlong.dao.TypeDao;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Lenovo
 */
public class DlgAddType extends JDialog {
    public DlgAddType(Window owner) {
        super(owner);
        initComponents();
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        String name = txtName.getText();
        String remark = txtRemark.getText();
        if (name.length() == 0) {
            JOptionPane.showMessageDialog(this, "请输入正确的类别");
            return;
        }
        TypeDao typeDao = new TypeDao();
        if (typeDao.addType(name, remark)) {
            JOptionPane.showMessageDialog(this, "成功添加类别");
        }
        dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - cuixinlong
        label1 = new JLabel();
        label2 = new JLabel();
        txtName = new JTextField();
        scrollPane1 = new JScrollPane();
        txtRemark = new JTextArea();
        btnAdd = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7c7b\u522b");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7c7b\u522b\u540d\u79f0");
        contentPane.add(label1);
        label1.setBounds(120, 65, 65, 25);

        //---- label2 ----
        label2.setText("\u7c7b\u522b\u8bf4\u660e");
        contentPane.add(label2);
        label2.setBounds(120, 140, 60, 30);
        contentPane.add(txtName);
        txtName.setBounds(205, 60, 155, txtName.getPreferredSize().height);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(txtRemark);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(205, 135, 175, 105);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0\u7c7b\u522b");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(105, 290), btnAdd.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(350, 285, 78, 34);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - cuixinlong
    private JLabel label1;
    private JLabel label2;
    private JTextField txtName;
    private JScrollPane scrollPane1;
    private JTextArea txtRemark;
    private JButton btnAdd;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
